﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace MipsPipelineSimulator
{
    public partial class MemoryControl : UserControl
    {
        public RichListView memoryListView = null; 

        public MemoryControl()
        {
            InitializeComponent();
        }

        private void MemoryControl_Load(object sender, EventArgs e)
        {
            memoryListView = new RichListView(2,true);
            ColumnHeader name = new ColumnHeader();
            name.Text = "地址";
            name.Width = 123;
            ColumnHeader value = new ColumnHeader();
            value.Text = "值";
            value.Width = 123;
            memoryListView.Columns.AddRange(new ColumnHeader[2] { name, value });
            memoryListView.Dock = DockStyle.Fill;
            memoryListView.Location = new Point(0, 0);
            this.Controls.Add(memoryListView);

            memoryListView.SetColumnEditable(2, new RichListView.validate(this.validateValue));
            memoryListView.SetColumnEditable(1, new RichListView.validate(this.validateAddress));

            string[] s = new string[4];
            s[0] = "0x11000000";
            s[1] = "0x12345";
            ListViewItem l1 = new ListViewItem(s);
            memoryListView.Items.Add(l1);

            s[0] = "0x11000001";
            s[1] = "0x12345";
            memoryListView.Items.Add(new ListViewItem(s));

            s[0] = "0x11000002";
            s[1] = "0x12345";
            memoryListView.Items.Add(new ListViewItem(s));

            s[0] = "0x11000003";
            s[1] = "0x12345";
            memoryListView.Items.Add(new ListViewItem(s));
        }

        public bool validateValue(string inputStr)
        {
            Regex r = new Regex(@"^(0x|0X)[0-9A-Fa-f]{1,8}$");
            return r.Match(inputStr).Success;
        }

        public bool validateAddress(string inputStr)
        {
            Regex r = new Regex(@"^(0x|0X)[0-9A-Fa-f]{1,8}$");
            return r.Match(inputStr).Success;
        }
    }
}
